const {
  getHeaderHeight
} = require('@/utils/util.js')
const appInstance=getApp();
const BASE_URL= appInstance.globalData.baseUrl;
Page({
  data: {
    baseUrl: BASE_URL,
    headerHeight: getHeaderHeight().headerHeight,
    swiperList: [{
        id: 1,
        image: BASE_URL + '/images/splash1.webp', // 替换为你的图片路径
        title: '欢迎使用我们的小程序',
        desc: '为您提供便捷的服务和体验'
      },
      {
        id: 2,
        image: BASE_URL + '/images/splash2.webp',
        title: '全新界面设计',
        desc: '简洁直观，操作流畅'
      },
      {
        id: 3,
        image: BASE_URL + '/images/splash3.webp',
        title: '更多功能即将上线',
        desc: '敬请期待！'
      }
    ],
    indicatorDots: false, // 隐藏默认指示点
    autoplay: true,
    interval: 5000,
    duration: 500,
    current: 1,
    progressWidth: 33.33 // 初始进度宽度
  },

  onLoad() {
    // 监听Swiper变化
    this.setData({
      swiperListLength: this.data.swiperList.length
    });

    // 启动定时器更新进度条
    this.progressInterval = setInterval(() => {
      if (this.data.current < this.data.swiperList.length) {
        const nextCurrent = this.data.current + 1;
        const progressWidth = (nextCurrent / this.data.swiperList.length) * 100;
        this.setData({
          current: nextCurrent,
          progressWidth: progressWidth
        });
      } else {
        clearInterval(this.progressInterval);
      }
    }, this.data.interval);
  },

  onSwiperChange(e) {
    const index = e.detail.current + 1;
    const progressWidth = (index / this.data.swiperList.length) * 100;
    if(index==this.data.swiperList.length){
      setTimeout(() => {
        this.skipWelcome();
      }, 1750);
    }
    this.setData({
      current: index,
      progressWidth: progressWidth
    });
  },

  skipWelcome() {
    // 清除定时器
    clearInterval(this.progressInterval);

    // 跳转到首页或其他页面
    wx.reLaunch({
      url: '/pages/home/index'
    });
  },

  onUnload() {
    // 页面卸载时清除定时器
    clearInterval(this.progressInterval);
  }
});